//
//  LJP_ DetailsViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/30.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_ DetailsViewController.h"
#import "LJP_ DetailsHearderModel.h"
#import "LJP_RecommendDetailsView.h"
#import "LJP_DetailsHeadView.h"
#import "LJP_CustomTableViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "LJP_DaysModel.h"
#import "LJP_ CustomHeaderView.h"
@interface LJP__DetailsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) LJP_DetailsHeadView *headView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) LJP__DetailsHearderModel *headModel;
@property (nonatomic, strong) NSArray *daysArray;
@end

@implementation LJP__DetailsViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.headView = [[LJP_DetailsHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    [self.view addSubview:_headView];
    [self.headView.cancle addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    
    [self tableViewSubview];
    [self dataHandle];
}
- (void)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//[UIColor colorWithRed:1.000 green:0.999 blue:0.911 alpha:1.000];
    [self.view addSubview:_tableView];
    self.tableView.separatorStyle = 0;
}
- (void)dataHandle
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"http://api.breadtrip.com/trips/%@/waypoints/", self.stringId);
    [manager GET:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", self.stringId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.headModel = [LJP__DetailsHearderModel detailsWithDic:responseObject];
        self.daysArray = [responseObject objectForKey:@"days"];
        [self.tableView reloadData];
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.daysArray.count + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return [[self.daysArray[section - 1] objectForKey:@"waypoints"
                 ] count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zxc"];
    if (cell == nil) {
        cell = [[LJP_CustomTableViewCell alloc] initWithStyle:0 reuseIdentifier:@"zxc"];
    }
    LJP_DaysModel *model = [[LJP_DaysModel alloc] init];
    NSDictionary *dic = [[self.daysArray[indexPath.section - 1] objectForKey:@"waypoints"
                          ] objectAtIndex:indexPath.row];
    [model setValuesForKeysWithDictionary:dic];
    cell.model = model;
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        LJP_RecommendDetailsView *recommend = [[LJP_RecommendDetailsView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 325)];
        recommend.model = self.headModel;
        return recommend;
    }else{
        LJP__CustomHeaderView *headView = [[LJP__CustomHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        NSDictionary *dic = self.daysArray[section - 1];
        headView.days.text = [NSString stringWithFormat:@"%@    第%@天", dic[@"date"], dic[@"day"]];
        return headView;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 325;
    }
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /*计算高度的重点:
     * 1. 预定义的矩形宽度 要和sell 中显示 文字的试图宽度一致
     * 2. 字体大小要和cell以指
     */
    NSDictionary *dic = [[self.daysArray[indexPath.section - 1] objectForKey:@"waypoints"
                          ] objectAtIndex:indexPath.row];
    
    NSString *str = dic[@"text"];
    CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//    UIImageView *temp = [[UIImageView alloc] init];
//    [temp sd_setImageWithURL:[NSURL URLWithString:imageStr]];
    NSString *imageStr = dic[@"photo"];
    if (imageStr.length != 0) {
        NSDictionary *info = dic[@"photo_info"];
        NSNumber *wi = info[@"w"];
        NSNumber *hi = info[@"h"];
        float w = wi.floatValue;
        float h = hi.floatValue;

        
        return rect.size.height + ([UIScreen mainScreen].bounds.size.width - 30) / w * h + 40;
    }
    return rect.size.height + 40;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
